from django.db import models
from django.contrib.auth.models import User


class Post(models.Model):
    """匿名树洞帖子表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID(匿名展示)'
    )
    title = models.CharField(
        max_length=200, 
        null=True, 
        blank=True, 
        verbose_name='标题',
        db_comment='帖子标题'
    )
    content = models.TextField(
        verbose_name='内容',
        db_comment='帖子正文内容'
    )
    emotion_tags = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='情绪标签',
        db_comment='帖子情绪标签JSON数组'
    )
    is_anonymous = models.BooleanField(
        default=True, 
        verbose_name='是否匿名',
        db_comment='是否匿名发帖'
    )
    view_count = models.IntegerField(
        default=0, 
        verbose_name='浏览数',
        db_comment='帖子浏览次数'
    )
    like_count = models.IntegerField(
        default=0, 
        verbose_name='点赞数',
        db_comment='帖子点赞数'
    )
    status = models.CharField(
        max_length=20, 
        default='published', 
        verbose_name='状态',
        db_comment='帖子状态(published/hidden/deleted)'
    )
    audit_status = models.CharField(
        max_length=20, 
        default='pending', 
        verbose_name='审核状态',
        db_comment='内容审核状态(pending/approved/rejected)'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_community_post'
        db_table_comment = '匿名树洞帖子表'
        verbose_name = '树洞帖子'
        verbose_name_plural = '树洞帖子'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.id} - {self.title or "无标题"}'


class Comment(models.Model):
    """帖子评论表"""
    post = models.ForeignKey(
        Post, 
        on_delete=models.CASCADE, 
        related_name='comments',
        verbose_name='帖子',
        db_comment='关联帖子ID'
    )
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    content = models.TextField(
        verbose_name='评论内容',
        db_comment='评论正文内容'
    )
    is_anonymous = models.BooleanField(
        default=True, 
        verbose_name='是否匿名',
        db_comment='是否匿名评论'
    )
    is_warm_response = models.BooleanField(
        default=False, 
        verbose_name='是否温暖回应',
        db_comment='是否标记为温暖回应'
    )
    like_count = models.IntegerField(
        default=0, 
        verbose_name='点赞数',
        db_comment='评论点赞数'
    )
    audit_status = models.CharField(
        max_length=20, 
        default='pending', 
        verbose_name='审核状态',
        db_comment='内容审核状态'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_community_comment'
        db_table_comment = '帖子评论表'
        verbose_name = '帖子评论'
        verbose_name_plural = '帖子评论'
        ordering = ['created_at']

    def __str__(self):
        return f'{self.post.id} - 评论 - {self.created_at}'


class SupportGroup(models.Model):
    """互助小组表"""
    group_name = models.CharField(
        max_length=200, 
        verbose_name='小组名称',
        db_comment='互助小组名称'
    )
    description = models.TextField(
        verbose_name='小组描述',
        db_comment='小组介绍和目标'
    )
    topic = models.CharField(
        max_length=100, 
        verbose_name='主题',
        db_comment='小组主题(焦虑/抑郁/失眠等)'
    )
    creator = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        related_name='created_groups',
        verbose_name='创建者',
        db_comment='小组创建者ID'
    )
    members = models.ManyToManyField(
        User, 
        related_name='joined_groups', 
        verbose_name='成员',
        db_comment='小组成员'
    )
    max_members = models.IntegerField(
        default=10, 
        verbose_name='最大人数',
        db_comment='小组最大成员数'
    )
    is_active = models.BooleanField(
        default=True, 
        verbose_name='是否活跃',
        db_comment='小组是否活跃'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_support_group'
        db_table_comment = '互助小组表'
        verbose_name = '互助小组'
        verbose_name_plural = '互助小组'

    def __str__(self):
        return self.group_name
